package com.gdpu.project.dao.impl;

import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gdpu.project.dao.ProApprovalDao;
import com.gdpu.project.vo.ProApproval;

/**
 * A data access object (DAO) providing persistence and search support for
 * ProApproval entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.gdpu.project.vo.ProApproval
 * @author MyEclipse Persistence Tools
 */

public class ProApprovalDaoImpl extends HibernateDaoSupport implements ProApprovalDao {
	private static final Log log = LogFactory.getLog(ProApprovalDaoImpl.class);
	// property constants
	public static final String PROJECT_ID = "projectId";
	public static final String TEACHER_ID = "teacherId";
	public static final String PRO_ORIGIN = "proOrigin";
	public static final String PRO_FUNDS = "proFunds";
	public static final String STATE = "state";
	public static final String COMMENT = "comment";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProApprovalDao#save(com.gdpu.project.vo.ProApproval)
	 */
	public void save(ProApproval transientInstance) {
		log.debug("saving ProApproval instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProApprovalDao#delete(com.gdpu.project.vo.ProApproval)
	 */
	public void delete(ProApproval persistentInstance) {
		log.debug("deleting ProApproval instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProApprovalDao#findById(java.lang.Integer)
	 */
	public ProApproval findById(java.lang.Integer id) {
		log.debug("getting ProApproval instance with id: " + id);
		try {
			ProApproval instance = (ProApproval) getHibernateTemplate().get(
					"com.gdpu.project.vo.ProApproval", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProApprovalDao#findByExample(com.gdpu.project.vo.ProApproval)
	 */
	public List<ProApproval> findByExample(ProApproval instance) {
		log.debug("finding ProApproval instance by example");
		try {
			List<ProApproval> results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProApprovalDao#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List<ProApproval> findByProperty(String propertyName, Object value) {
		log.debug("finding ProApproval instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ProApproval as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProApprovalDao#findByProjectId(java.lang.Object)
	 */
	public List<ProApproval> findByProjectId(Object projectId) {
		return findByProperty(PROJECT_ID, projectId);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProApprovalDao#findByTeacherId(java.lang.Object)
	 */
	public List<ProApproval> findByTeacherId(Object teacherId) {
		return findByProperty(TEACHER_ID, teacherId);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProApprovalDao#findByProOrigin(java.lang.Object)
	 */
	public List<ProApproval> findByProOrigin(Object proOrigin) {
		return findByProperty(PRO_ORIGIN, proOrigin);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProApprovalDao#findByProFunds(java.lang.Object)
	 */
	public List<ProApproval> findByProFunds(Object proFunds) {
		return findByProperty(PRO_FUNDS, proFunds);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProApprovalDao#findByState(java.lang.Object)
	 */
	public List<ProApproval> findByState(Object state) {
		return findByProperty(STATE, state);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProApprovalDao#findByComment(java.lang.Object)
	 */
	public List<ProApproval> findByComment(Object comment) {
		return findByProperty(COMMENT, comment);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProApprovalDao#findAll()
	 */
	public List<ProApproval> findAll() {
		log.debug("finding all ProApproval instances");
		try {
			String queryString = "from ProApproval";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProApprovalDao#merge(com.gdpu.project.vo.ProApproval)
	 */
	public ProApproval merge(ProApproval detachedInstance) {
		log.debug("merging ProApproval instance");
		try {
			ProApproval result = (ProApproval) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProApprovalDao#attachDirty(com.gdpu.project.vo.ProApproval)
	 */
	public void attachDirty(ProApproval instance) {
		log.debug("attaching dirty ProApproval instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.ProApprovalDao#attachClean(com.gdpu.project.vo.ProApproval)
	 */
	public void attachClean(ProApproval instance) {
		log.debug("attaching clean ProApproval instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ProApprovalDao getFromApplicationContext(
			ApplicationContext ctx) {
		return (ProApprovalDao) ctx.getBean("ProApprovalDAO");
	}
}